//
//  DFCompseView.swift
//  新浪微博
//
//  Created by dafei on 16/5/28.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class DFCompseView: UIView {
    
    //按钮数组的保存按钮
    private lazy var buttons: [DFCompseButton] = [DFCompseButton]()
    //保存参数 方便其他地方使用
    //为了防止循环引用 加weak
    weak var target: UIViewController?
    
    func show(targetVC target: UIViewController) {
    
        self.target = target
        //添加视图
        target.view.addSubview(self)
    
    }
    
    
    override init(frame: CGRect) {
        //制定大小
        let rect = UIScreen.mainScreen().bounds
        super.init(frame: rect)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        // 从父视图中移除
//        self.removeFromSuperview()
        //index 相当于btn中枚举的角标(0.1.2...)
        for (index,btn) in buttons.enumerate() {
        
            startAnimation(btn, index: index, offset: 350)
        
        }
        
        
    }
    //将要移动到父视图上之后开始做动画
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(superview)
        for (index,btn) in buttons.enumerate() {
        
            startAnimation(btn, index: index, offset: -350)
        
        }
        
    }
    
    //按钮的动画
    private func startAnimation(btn: DFCompseButton, index: Int, offset: CGFloat) {
    
        UIView.animateWithDuration(0.8, delay: Double(index) * 0.02, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: [], animations: { 
            // 修改按钮的y 的值
            btn.center = CGPoint(x: btn.center.x, y: btn.center.y + offset)
            
            }) { (_) in
                if offset > 0 {
                //移除
                    self.removeFromSuperview()
                
                }
        }
   
    }
    
    
    
    
    
    //设置UI界面
    private func setupUI() {
        //设置背景颜色
        backgroundColor = UIColor.redColor()
        addSubview(backImageView)
        addSubview(sloganImageView)
        
        
        sloganImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(self.snp_top).offset(100)
        }
        
        //添加按钮
        addChildButtons()
    }
    
    //布局按钮视图
    private func addChildButtons() {
    
        let cout = composeItems.count
        //指定按钮的大小
        let w: CGFloat = 80
        let h: CGFloat = 120
        let margin: CGFloat = (ScreenWidth - 3 * w) / 4
        for i in 0..<cout {
        
            let btn = DFCompseButton()
            //设置按钮的图片和文字
            let item = composeItems[i]
            btn.composeItem = item
//            btn.setImage(UIImage(named: item.icon ?? ""), forState: .Normal)
//            btn.setTitle(item.title, forState: .Normal)
//            btn.titleLabel?.textAlignment = .Center
//            btn.titleLabel?.font = UIFont.systemFontOfSize(14)
//            btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
            
            //添加点击事件
            btn.addTarget(self, action: #selector(DFCompseView.btnDidClick(_:)), forControlEvents: .TouchUpInside)
            
            //指定frame
            let row = i / 3
            let col = i % 3
            let x = CGFloat(col) * (margin + w) + margin
            let y = CGFloat(row) * (h + margin) + ScreenHeight
            
            btn.frame = CGRect(x: x, y: y, width: w, height: h)
            // btn.backgroundColor = UIColor.redColor()
            self.addSubview(btn)
            //添加到数组中
            buttons.append(btn)
            
        
        }
   
    }
    
    //被点击的按钮放大  其他的缩小
    @objc private func btnDidClick(btn: DFCompseButton) {
    
        for button in buttons {
        
            //判断是否 点击的按钮
            var scale: CGFloat = 0
            if button == btn {
                //放大
                scale = 2.0
            
            } else {
                //缩小
                scale = 0.2
            
            }
     
        UIView.animateWithDuration(0.25, animations: {
            //改变形态
            button.alpha = 0
            
            button.transform = CGAffineTransformMakeScale(scale, scale)
            
        }) { (_) in
                print("ok")
            }
    
        }
        //跳转页面
        //获取控制的名称
        print(btn.composeItem)
        guard let target = btn.composeItem?.target else {
        
           return
        }
        
        //就是根据字符串来获取控制器对应的类型
        let type = NSClassFromString(target) as! UIViewController.Type
        //初始化控制器
        let vc = type.init()
        let nav = UINavigationController(rootViewController: vc)
        //页面跳转
        self.target?.presentViewController(nav, animated: false, completion: {
            self.removeFromSuperview()
        })
    
    }
        
   
    
    
    //懒加载控件
    //高斯模糊视图
    private lazy var backImageView: UIImageView = UIImageView (image: UIImage.snapShontCurrentWindow().applyLightEffect())
    private lazy var sloganImageView: UIImageView = UIImageView(image:UIImage(named: "compose_slogan"))
    
    private lazy var composeItems: [DFComposeItem] = {
    
        var arry = [DFComposeItem]()
        //获取文件的路径
        let path = NSBundle.mainBundle().pathForResource("Compose.plist", ofType: nil)!
        //读取路径的数据
        let itemArry = NSArray(contentsOfFile: path) as! [[String : AnyObject]]
        //遍历数组 字典转模型
        for item in itemArry {
        
            let composeItem = DFComposeItem(dict: item)
            arry.append(composeItem)
        
        }
        return arry
    
    }()
    
    

}
